#include <rw/dec52.h> /* RWDecimal52 */ #include <rw/dec64.h> /* RWDecimal64 */ #include <rw/dec96.h> /* RWDecimal96 */ RWDecimal64 x = "0.01"; cout << "one dollar: " << 100*x << endl;
RWDecimals are exact representations of decimal fractions. They behave very similarly to the built-in floating point types, float and double. The difference is that since the built-in types use base 2, they cannot store decimal fractions exactly and so loss of precision and rounding errors occur. On the other hand, the RWDecimal classes use base 10 and so they can do decimal math exactly.
Three different RWDecimal classes are provided: RWDecimal52, RWDecimal64, and RWDecimal96. They vary in the amount of precision provided, as described below in the "Limits" section. The tradeoff is simple: the more precision, the slower the class. Throughout this section, when we refer to a class simply using RWDecimal, you can assume that it applies to any of the three classes.
Return to Top of File.#include <rw/decio.h> main() { cout << RWDecimalFormat("$_____.__")("2.243") << endl; }
The RWDecimalFormat class encapsulates formatting information for converting a decimal number to a string. You can construct and modify the attributes of a formatted object in two ways:
You can also use a hybrid approach by initially constructing an RWDecimalFormat object using a picture string and then "fine tuning" the formatting parameters using the member functions to change attributes directly.
Once a formatted object is constructed, you use the function call operator() to convert from decimal numbers to strings.
Return to Top of File.#include <rw/dec52.h> /* RWDecimal52InexactErr, RWDecimal52OverflowErr*/ #include <rw/dec64.h> /* RWDecimal64InexactErr, RWDecimal64OverflowErr*/ #include <rw/dec96.h> /* RWDecimal96InexactErr, RWDecimal96OverflowErr*/ RWDecimal52 x = "9999999999"; RWDecimal52 y = x*x; // throws an RWDecimal52OverflowErr RWDecimal52 a = "9.9999999999"; RWDecimal52 b = a*a; // throws an RWDecimal52InExactErr
RWDecimalErrs are error objects used to signal problems in computations involving RWDecimal objects. See the "arithmetic" section of the RWDecimal and RWFixedDecimal reference sections for details on when these objects are used and how to set error handlers.
Return to Top of File.#include <rw/fixdec52.h> /* RWFixedDecimal52 */ #include <rw/fixdec64.h> /* RWFixedDecimal64 */ #include <rw/fixdec96.h> /* RWFixedDecimal96 */ RWFixedDecimal64 dollarAccount(0,2); // init to zero, // 2 decimal places
RWFixedDecimals are exact representations of decimal fractions with a fixed number of digits after the decimal point. In most ways, they behave exactly like the corresponding RWDecimal classes. The exception is that rounding automatically occurs to insure the correct number of decimal places.
Three different RWFixedDecimal classes are provided: RWFixedDecimal52, RWFixedDecimal64 , and RWFixedDecimal96. They vary in the amount of precision provided, as described below in the "Limits" section. The tradeoff is simple: the more precision, the slower the class. Throughout this section, the term RW FixedDecimal applies to any of the three classes.
Return to Top of File.